package com.ktgj.hotel.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ktgj.common.core.utils.SnowflakeUtils;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Employee;
import com.ktgj.hotel.domain.HotelRoomPrice;
import com.ktgj.hotel.domain.common.HotelInfo;
import com.ktgj.hotel.domain.common.Hotels;
import com.ktgj.hotel.domain.dic.DicCity;
import com.ktgj.hotel.mapper.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.hotel.domain.HotelRoom;
import com.ktgj.hotel.service.IHotelRoomService;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;

/**
 * 酒店房型信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-25
 */
@Service
public class HotelRoomServiceImpl implements IHotelRoomService 
{
    @Autowired
    private HotelRoomMapper hotelRoomMapper;
    @Autowired
    private RemoteCustomerService remoteCustomerService;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private DicCityMapper dicCityMapper;
    @Autowired
    private HotelInfoMapper hotelInfoMapper;
    @Autowired
    private HotelRoomPriceMapper hotelRoomPriceMapper;

    /**
     * 查询酒店房型信息
     * 
     * @param roomId 酒店房型信息ID
     * @return 酒店房型信息
     */
    @Override
    public HotelRoom selectHotelRoomById(Long roomId)
    {
        return hotelRoomMapper.selectHotelRoomById(roomId);
    }

    /**
     * 查询酒店房型信息列表
     * 
     * @param hotelRoom 酒店房型信息
     * @return 酒店房型信息
     */
    @Override
    public List<HotelRoom> selectHotelRoomList(HotelRoom hotelRoom)
    {
        return hotelRoomMapper.selectHotelRoomList(hotelRoom);
    }

    /**
     * 新增酒店房型信息
     * 
     * @param hotelRoom 酒店房型信息
     * @return 结果
     */
    @Override
    public int insertHotelRoom(HotelRoom hotelRoom)
    {
        return hotelRoomMapper.insertHotelRoom(hotelRoom);
    }

    /**
     * 修改酒店房型信息
     * 
     * @param hotelRoom 酒店房型信息
     * @return 结果
     */
    @Override
    public int updateHotelRoom(HotelRoom hotelRoom)
    {
        return hotelRoomMapper.updateHotelRoom(hotelRoom);
    }

    /**
     * 批量删除酒店房型信息
     * 
     * @param roomIds 需要删除的酒店房型信息ID
     * @return 结果
     */
    @Override
    public int deleteHotelRoomByIds(Long[] roomIds)
    {
        return hotelRoomMapper.deleteHotelRoomByIds(roomIds);
    }

    /**
     * 删除酒店房型信息信息
     * 
     * @param roomId 酒店房型信息ID
     * @return 结果
     */
    @Override
    public int deleteHotelRoomById(Long roomId)
    {
        return hotelRoomMapper.deleteHotelRoomById(roomId);
    }

    /**
     * 新增酒店信息
     * */
    @Transactional
    @Override
    public int insertHotel(HttpServletRequest request, Hotels hotels) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String token = request.getHeader("token");
        Employee employee = remoteCustomerService.getLoginEmployeeByToken(token).getData();
        if (employee == null){
            throw new Exception("登录已过期");
        }

        SnowflakeUtils snowflakeUtils = new SnowflakeUtils();
        Long id = snowflakeUtils.nextId();
        hotels.setId(id);

        if (hotels.getCityName() != null && !hotels.getCityName().equals("")){
            DicCity dicCity = dicCityMapper.dicCityLngAnfLat(hotels.getCityName());
            if (dicCity != null){
                hotels.setCityId(dicCity.getId());
            }
        }

        hotels.setUpdateTime(new Date());
        hotels.setCreateTime(new Date());

        int i = hotelMapper.insertHotel(hotels);
        if (i <= 0){
            throw new Exception("添加酒店信息失败");
        }

        if (hotels.getHotelInfo() != null){
            hotels.getHotelInfo().setHotelId(hotels.getId());
            hotels.getHotelInfo().setUpdateTime(new Date());
            hotels.getHotelInfo().setCreateTime(new Date());
            i = hotelInfoMapper.insertHotelInfo(hotels.getHotelInfo());
            if (i <= 0){
                throw new Exception("添加酒店详情信息失败");
            }

            if (hotels.getHotelInfo().getHotelRoomList() != null && hotels.getHotelInfo().getHotelRoomList().size() > 0){
                for (HotelRoom hotelRoom : hotels.getHotelInfo().getHotelRoomList()) {
                    hotelRoom.setHotelInfoId(hotels.getHotelInfo().getId());
                    hotelRoom.setRoomCreatetime(new Date());
                    hotelRoom.setRoomCreateby(employee.getEmployeeId());
                    i = hotelRoomMapper.insertHotelRoom(hotelRoom);
                    if (i <= 0){
                        throw new Exception("添加酒店房型信息" + hotelRoom.getRoomName() + "失败");
                    }

                    if (hotelRoom.getHotelRoomPriceList() != null && hotelRoom.getHotelRoomPriceList().size() > 0){
                        for (HotelRoomPrice hotelRoomPrice : hotelRoom.getHotelRoomPriceList()) {
                            if (hotelRoomPrice.getBeginDate() != null){
                                hotelRoomPrice.setBeginTime(format.parse(hotelRoomPrice.getBeginDate()));
                            }
                            if (hotelRoomPrice.getEndDate() != null){
                                hotelRoomPrice.setEndTime(format.parse(hotelRoomPrice.getEndDate()));
                            }

                            hotelRoomPrice.setRoomId(hotelRoom.getRoomId());
                            hotelRoomPrice.setCreateTime(new Date());
                            hotelRoomPrice.setCreateBy(employee.getEmployeeId());
                            i = hotelRoomPriceMapper.insertHotelRoomPrice(hotelRoomPrice);
                            if (i <= 0){
                                throw new Exception("添加酒店房型价格信息失败");
                            }
                        }
                    }
                }
            }
        }

        return 1;
    }

    /**
     * 查询待修改的酒店信息
     * */
    @Override
    public Hotels hotelDetails(String type, Long id) {
        Hotels hotels = null;
        if (type.equals("hotel")){
            hotels = hotels(id);
        }
        else if (type.equals("hotelInfo")){
            hotels = hotelInfo(id);
        }
        else if (type.equals("hotelRoom")){
            hotels = hotelRoom(id);
        }
        else if (type.equals("price")){
            hotels = hotelRoomPrice(id);
        }
        return hotels;
    }

    /**
     * 修改酒店信息
     * */
    @Transactional
    @Override
    public int updateHotel(Hotels hotels) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        hotels.setUpdateTime(new Date());

        int i = hotelMapper.updateHotel(hotels);
        if (i <= 0){
            throw new Exception("修改酒店信息失败");
        }

        if (hotels.getHotelInfo() != null){
            hotels.getHotelInfo().setUpdateTime(new Date());
            i = hotelInfoMapper.updateHotelInfo(hotels.getHotelInfo());
            if (i <= 0){
                throw new Exception("修改酒店详情信息失败");
            }

            if (hotels.getHotelInfo().getHotelRoomList() != null && hotels.getHotelInfo().getHotelRoomList().size() > 0){
                for (HotelRoom hotelRoom : hotels.getHotelInfo().getHotelRoomList()) {
                    if (hotelRoom.getRoomId() != null && hotelRoom.getRoomId() != 0 && !hotelRoom.getRoomId().equals("")){
                        i = hotelRoomMapper.updateHotelRoom(hotelRoom);
                    }
                    else {
                        i = hotelRoomMapper.insertHotelRoom(hotelRoom);
                    }
                    if (i <= 0){
                        throw new Exception("修改酒店房型信息" + hotelRoom.getRoomName() + "失败");
                    }

                    if (hotelRoom.getHotelRoomPriceList() != null && hotelRoom.getHotelRoomPriceList().size() > 0){
                        for (HotelRoomPrice hotelRoomPrice : hotelRoom.getHotelRoomPriceList()) {
                            if (hotelRoomPrice.getBeginDate() != null){
                                hotelRoomPrice.setBeginTime(format.parse(hotelRoomPrice.getBeginDate()));
                            }
                            if (hotelRoomPrice.getEndDate() != null){
                                hotelRoomPrice.setEndTime(format.parse(hotelRoomPrice.getEndDate()));
                            }

                            if (hotelRoomPrice.getRoomPriceId() != null && hotelRoomPrice.getRoomPriceId() != 0 && !hotelRoomPrice.getRoomPriceId().equals("")){
                                i = hotelRoomPriceMapper.updateHotelRoomPrice(hotelRoomPrice);
                            }
                            else {
                                i = hotelRoomPriceMapper.insertHotelRoomPrice(hotelRoomPrice);
                            }
                            if (i <= 0){
                                throw new Exception("修改酒店房型价格信息失败");
                            }
                        }
                    }
                }
            }
        }

        return 1;
    }

    /**
     * 酒店详情
     * */
    @Override
    public HotelInfo hotelInfoDetails(Long id) {
        HotelInfo hotelInfo = hotelInfoMapper.hotelInfoDetails(id);
        return hotelInfo;
    }

    /**
     * 酒店房型列表
     * */
    @Override
    public List<HotelRoom> selHotelRoom(Long id) {
        List<HotelRoom> hotelRoomList = hotelRoomMapper.selHotelRoom(id);
        return hotelRoomList;
    }

    /**
     * 酒店房型价格列表
     * */
    @Override
    public List<HotelRoomPrice> roomPrice(Long id) {
        List<HotelRoomPrice> hotelRoomPriceList = hotelRoomPriceMapper.roomPrice(id);
        return hotelRoomPriceList;
    }

    /**
     * 查询酒店信息
     * */
    public Hotels hotels(Long id){
        Hotels hotels = hotelMapper.selectHotelById(id);
        return hotels;
    }

    /**
     * 查询酒店详情信息
     * */
    public Hotels hotelInfo(Long id){
        HotelInfo hotelInfo = hotelInfoMapper.selectHotelInfoById(id);

        Hotels hotels = hotels(hotelInfo.getHotelId());
        hotels.setHotelInfo(hotelInfo);
        return hotels;
    }

    /**
     * 查询酒店房型信息
     * */
    public Hotels hotelRoom(Long id){
        HotelRoom hotelRoom = hotelRoomMapper.selectHotelRoomById(id);

        Hotels hotels = hotelInfo(hotelRoom.getHotelInfoId());
        List<HotelRoom> hotelRoomList = new ArrayList<>();
        hotelRoomList.add(hotelRoom);
        hotels.getHotelInfo().setHotelRoomList(hotelRoomList);
        return hotels;
    }

    /**
     * 查询酒店房型价格信息
     * */
    public Hotels hotelRoomPrice(Long id){
        HotelRoomPrice hotelRoomPrice = hotelRoomPriceMapper.selectHotelRoomPriceById(id);

        Hotels hotels = hotelRoom(hotelRoomPrice.getRoomId());
        List<HotelRoomPrice> hotelRoomPriceList = new ArrayList<>();
        hotelRoomPriceList.add(hotelRoomPrice);
        for (HotelRoom hotelRoom : hotels.getHotelInfo().getHotelRoomList()) {
            hotelRoom.setHotelRoomPriceList(hotelRoomPriceList);
            break;
        }
        return hotels;
    }
}
